home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / dwmainc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  5.7 KB  |  252 lines

  1. /* Copyright (C) 1996-2000, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19.  
  20. // $Id: dwmainc.cpp,v 1.3 2000/09/19 19:00:10 lpd Exp $
  21. // Ghostscript DLL loader for Windows 95/NT
  22. // For WINDOWCOMPAT (console mode) application
  23.  
  24. #define STRICT
  25. #include <windows.h>
  26. #include <shellapi.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <dos.h>
  31. #include <fcntl.h>
  32. #include <io.h>
  33. extern "C" {
  34. #include "gscdefs.h"
  35. #define GSREVISION gs_revision
  36. #include "gsdll.h"
  37. }
  38. #include "dwmain.h"
  39. #include "dwdll.h"
  40.  
  41. #if defined(_MSC_VER) && defined(__WIN32__)
  42. #define _export
  43. #endif
  44.  
  45. /* public handles */
  46. HINSTANCE ghInstance;
  47.  
  48. const char *szDllName = "GSDLL32.DLL";
  49.  
  50.  
  51. int FAR _export gsdll_callback(int message, char FAR *str, unsigned long count);
  52.  
  53. // the Ghostscript DLL class
  54. gsdll_class gsdll;
  55.  
  56. char start_string[] = "systemdict /start get exec\n";
  57.  
  58. // program really starts at WinMain
  59. int
  60. new_main(int argc, char *argv[])
  61. {
  62. typedef char FAR * FARARGV_PTR;
  63. int rc;
  64.  
  65.     setmode(fileno(stdin), O_BINARY);
  66.     setmode(fileno(stdout), O_BINARY);
  67.  
  68.     // load DLL
  69.     if (gsdll.load(ghInstance, szDllName, GSREVISION)) {
  70.     char buf[256];
  71.     gsdll.get_last_error(buf, sizeof(buf));
  72.     fputs(buf, stdout);
  73.     return 1;
  74.     }
  75.  
  76.     // initialize the interpreter
  77.     rc = gsdll.init(gsdll_callback, (HWND)NULL, argc, argv);
  78.     if (rc == GSDLL_INIT_QUIT) {
  79.         gsdll.unload();
  80.     return 0;
  81.     }
  82.     if (rc) {
  83.     char buf[256];
  84.     gsdll.get_last_error(buf, sizeof(buf));
  85.     fputs(buf, stdout);
  86.         gsdll.unload();
  87.     return rc;
  88.     }
  89.  
  90.     // if (!batch)
  91.     gsdll.execute(start_string, strlen(start_string));
  92.     
  93.     gsdll.unload();
  94.  
  95.     return 0;
  96. }
  97.  
  98.  
  99. #if defined(_MSC_VER)
  100. /* MSVC Console EXE needs main() */
  101. int
  102. main(int argc, char *argv[])
  103. {
  104.     /* need to get instance handle */
  105.     ghInstance = GetModuleHandle(NULL);
  106.  
  107.     return new_main(argc, argv);
  108. }
  109. #else
  110. /* Borland Console EXE needs WinMain() */
  111. #pragma argsused  // ignore warning about unused arguments in next function
  112.  
  113. int PASCAL 
  114. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int cmdShow)
  115. {
  116. #define MAXCMDTOKENS 128
  117.     // BC++ 4.5 will give us _argc and _argv[], but they will be 
  118.     // incorrect if there is a space in the program name.
  119.     // Provide our own parsing code to create argc and argv[]. 
  120.     int argc;
  121.     LPSTR argv[MAXCMDTOKENS];
  122.     LPSTR p;
  123.     char command[256];
  124.     char *args;
  125.     char *d, *e;
  126.  
  127.     if (hPrevInstance) {
  128.     fputs("Can't run twice", stdout);
  129.     return FALSE;
  130.     }
  131.  
  132.     /* copy the hInstance into a variable so it can be used */
  133.     ghInstance = hInstance;
  134.  
  135.     // If called with "gswin32c.exe arg1 arg2"
  136.     // lpszCmdLine returns:
  137.     //    "arg1 arg2" if called with CreateProcess(NULL, command, ...)
  138.     //    "arg2"      if called with CreateProcess(command, args, ...)
  139.     // GetCommandLine() returns
  140.     //    ""gswin32c.exe" arg1 arg2" 
  141.     //          if called with CreateProcess(NULL, command, ...)
  142.     //    "  arg1 arg2"      
  143.     //          if called with CreateProcess(command, args, ...)
  144.     // Consequently we must use GetCommandLine() 
  145.     p = GetCommandLine();
  146.  
  147.     argc = 0;
  148.     args = (char *)malloc(lstrlen(p)+1);
  149.     if (args == (char *)NULL) {
  150.     fprintf(stdout, "Insufficient memory in WinMain()\n");
  151.     return 1;
  152.     }
  153.  
  154.    
  155.     // Parse command line handling quotes.
  156.     d = args;
  157.     while (*p) {
  158.     // for each argument
  159.  
  160.     if (argc >= MAXCMDTOKENS - 1)
  161.         break;
  162.  
  163.         e = d;
  164.         while ((*p) && (*p != ' ')) {
  165.         if (*p == '\042') {
  166.         // Remove quotes, skipping over embedded spaces.
  167.         // Doesn't handle embedded quotes.
  168.         p++;
  169.         while ((*p) && (*p != '\042'))
  170.             *d++ =*p++;
  171.         }
  172.         else 
  173.         *d++ = *p;
  174.         if (*p)
  175.         p++;
  176.         }
  177.     *d++ = '\0';
  178.     argv[argc++] = e;
  179.  
  180.     while ((*p) && (*p == ' '))
  181.         p++;    // Skip over trailing spaces
  182.     }
  183.     argv[argc] = NULL;
  184.  
  185.     if (strlen(argv[0]) == 0) {
  186.     GetModuleFileName(hInstance, command, sizeof(command)-1);
  187.     argv[0] = command;
  188.     }
  189.  
  190.     return new_main(argc, argv);
  191. }
  192. #endif
  193.  
  194.  
  195. int
  196. read_stdin(char FAR *str, int len)
  197. {
  198. int ch;
  199. int count = 0;
  200.     while (count < len) {
  201.     ch = fgetc(stdin);
  202.     if (ch == EOF)
  203.         return count;
  204.     *str++ = ch;
  205.     count++;
  206.     if (ch == '\n')
  207.         return count;
  208.     }
  209.     return count;
  210. }
  211.  
  212.  
  213. int FAR _export
  214. gsdll_callback(int message, char FAR *str, unsigned long count)
  215. {
  216. char buf[256];
  217.     switch (message) {
  218.     case GSDLL_POLL:
  219.         // Don't check message queue because we don't
  220.         // create any windows.
  221.         // May want to return error code if abort wanted
  222.         break;
  223.     case GSDLL_STDIN:
  224.         return read_stdin(str, count);
  225.     case GSDLL_STDOUT:
  226.         fwrite(str, 1, count, stdout);
  227.         fflush(stdout);
  228.         return count;
  229.     case GSDLL_DEVICE:
  230.         if (count) {
  231.         fputs("\n\
  232. The mswindll device is not supported by the command line version of\n\
  233. Ghostscript.  Select a different device using -sDEVICE= as described\n\
  234. in Use.htm.\n", stdout);
  235.         }
  236.         break;
  237.     case GSDLL_SYNC:
  238.         break;
  239.     case GSDLL_PAGE:
  240.         break;
  241.     case GSDLL_SIZE:
  242.         break;
  243.     default:
  244.         sprintf(buf,"Callback: Unknown message=%d\n",message);
  245.         fputs(buf, stdout);
  246.         break;
  247.     }
  248.     return 0;
  249. }
  250.  
  251.  
  252.